﻿using HospitalFollowUpSystem.DBC;
using MetroFramework.Forms;
using System;
using System.Windows.Forms;

namespace HospitalFollowUpSystem.View
{
    public partial class FrmAddTopicType : MetroForm
    {
        public FrmAddTopicType()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            LogicCheck();
        }

        //按回车键添加数据
        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                LogicCheck();
            }
        }

        //判断条件然后添加数据
        private void LogicCheck()
        {
            //问卷类型名
            var tableTypeName = txtName.Text.Trim();
            //插入成功标志
            var isSuccess = false;
            HRCM_DB db = new HRCM_DB();
            if (tableTypeName.Length > 0)
            {
                var trans = db.Database.BeginTransaction();
                try
                {
                    isSuccess = db.InertTopicGroup(tableTypeName, trans);
                    trans.Commit();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "操作提醒");
                    trans.Rollback();
                }
                finally
                {
                    trans.Dispose();
                }
                db.Database.Close();

                if (isSuccess)
                {
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败", "操作提醒");
                }
            }
            else
            {
                txtName.WaterMark = "不能为空";
            }
        }
    }
}
